//
//  ViewController.m
//  3DModelCollector
//
//  Created by Jing Zhang on 7/7/12.
//  Copyright (c) 2012 Tencent. All rights reserved.
//

#import "ViewController.h"
#import "ModelDataManager.h"
#import "BuildingViewController.h"
#import "BuildingTask.h"

typedef enum DisplayMode_ : NSUInteger {
    DM_Fresh,
    DM_Finished,
    DM_Draft,
    DM_All
}
DisplayMode;

@interface ViewController ()

@property (retain, nonatomic) IBOutlet UITableView *modelListView;

@property (retain, nonatomic) NSArray* displayedModelList;
@property (nonatomic) DisplayMode displayMode;

- (void)onSelectTaskType:(id)sender;

- (BOOL)loadData;

@end

@implementation ViewController

@synthesize modelListView;
@synthesize displayedModelList = _displayedModelList;
@synthesize displayMode = _displayMode;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"清单";
    }
    
    return self;
}

- (void)dealloc {
    
    self.displayedModelList = nil;
    [modelListView release];
    
    [super dealloc];
}

- (void)loadView {
    
    [super loadView];
    
    // Cusmize the title bar
    UISegmentedControl* filterSelector = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"未开始", @"已完成", @"草稿", @"全部", nil]];
    filterSelector.segmentedControlStyle = UISegmentedControlStyleBar;
    [filterSelector addTarget:self action:@selector(onSelectTaskType:) forControlEvents:UIControlEventValueChanged];
    filterSelector.selectedSegmentIndex = 0;
    self.navigationItem.titleView = filterSelector;
    [filterSelector release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    self.displayMode = DM_All;
    
    [self loadData];
}

- (void)viewDidUnload
{
    [self setModelListView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - [UITableViewDataSource]

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.displayedModelList.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString* cellID = @"ModelCell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    // Customize the cell
    cell.imageView.image = nil;
    cell.textLabel.text = [NSString stringWithFormat:@"No.%d", indexPath.row + 1];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"FID : nil"];
    
    return cell;
}

#pragma mark - [UITableViewDelegate]

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // View detail
    BuildingViewController* buildingViewController = [[BuildingViewController alloc] initWithNibName:@"BuildingViewController" bundle:nil];
    [self.navigationController pushViewController:buildingViewController animated:YES];
    [buildingViewController release];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)onSelectTaskType:(id)sender {
    
    UISegmentedControl* filterSelector = (UISegmentedControl*)sender;
    self.displayMode = filterSelector.selectedSegmentIndex;
    [self loadData];
}

#pragma mark - [Private methods]

- (BOOL)loadData {
    
    ModelDataManager* sharedInstance = [ModelDataManager sharedInstance];
    [sharedInstance loadTask];
    NSMutableArray* displayedList = [[NSMutableArray alloc] init];
    for (BuildingTask* oneTask in sharedInstance.taskList) {
        switch (self.displayMode) {
            case DM_All:
                [displayedList addObject:oneTask];
                break;
                
            case DM_Fresh:
                if (oneTask.taskState == TS_Fresh) {
                    [displayedList addObject:oneTask];
                }
                break;
                
            case DM_Draft:
                if (oneTask.taskState == TS_MissionImpossible ||
                    oneTask.taskState == TS_Draft) {
                    [displayedList addObject:oneTask];
                }
                break;
                
            case DM_Finished:
                if (oneTask.taskState == TS_Finished) {
                    [displayedList addObject:oneTask];
                }
                break;
                
            default:
                break;
        }
    }
    
    self.displayedModelList = displayedList;
    [displayedList release];
    
    [self.modelListView reloadData];
    
    return YES;
}

@end
